package lc.二叉树;

import utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/11
 */
public class Math104二叉树的最大深度 {

    // lc.递归
    public int maxDepth(TreeNode root) {

        if (root == null) {
            return 0;
        }
        int leftLevel = maxDepth(root.left);
        int rightLevel = maxDepth(root.right);
        return Math.max(leftLevel,rightLevel) + 1;
    }

    public int maxDepth1(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int level = 0;
        while (!queue.isEmpty()) {

            int size = queue.size();

            level++;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }

        }
        return level;
    }

}
